<template>
	<view class="container">
		<view class="preparationUpload-header">
			<view class="preparationUpload-header_upload" @click="upload">
				<image src="../../../static/imgs/mine/uploadschool.png"></image>
				<view class="text_black">上传文件</view>
			</view>
			<view class="text_tips preparationUpload-header_tips">您可以上传您的Word，PPT，Excel</view>
		</view>
		<view class="text_tips preparationUpload-tips" v-if="list.length>0">
			我已上传的文件
		</view>
		<view class="preparationUpload-list">
			<mark-slide-list :list="list" :button="buttonList" :border="true" @click="clickMethod"
				@change="changeMethod"></mark-slide-list>
		</view>
		<!-- 权限弹框 -->
		<view class="quanxian" v-if="quanShow">
			<view class="">
				存储空间/相机权限说明
			</view>
			<view class="">
				用于在添加、制作、上传、发布、分享、下载、搜索、识别图片和视频等场景中读取和写入相册和文件内容，拒绝将无法使用相关功能
			</view>
		</view>
		<xe-upload class="xe-upload" ref="XeUpload" :options="uploadOptions" @callback="handleUploadCallback"></xe-upload>
	</view>
</template>

<script>
	import config from "../../../static/js/config.js";
	import Cache from "../../../static/js/cache.js";
	import {
		getTeacherhelperDocs,
		getTeacherhelperDocsone,
		getTeacherhelperDocDel
	} from "../../../static/js/api/preparation.js";
	import markSlideList from '../../../components/mark-slide-list/mark-slide-list.vue'
	export default {
		components: {
			markSlideList
		},
		data() {
			return {
				// uploadOptions 参数跟uni.uploadFile的参数是一样的（除了类型为Function的属性）
				uploadOptions: {
					// url: '', // 不传入上传地址则返回本地链接
				},
				list: [], //doc_type 文档类型 0:其它 1:word 2:excel 3:ppt 4:pdf 5:txt 6:md
				buttonList: [{
					title: '删除',
					background: '#ff3b32'
				}],
				filePath: "",
				fileList: [],
				quanShow: false,
			}
		},
		onLoad() {
			this.uploadOptions = {
				url: config.baseUrl + '/teacherhelper/doc_upload',
				header: {
					'token': Cache.cache.get('token'),
				},
			}
			this.getData()
		},
		methods: {
			getData() {
				getTeacherhelperDocs({
					page: 1
				}).then(res => {
					console.log(res)
					if (res.docs) {
						res.docs.forEach(item => {
							item.slide_x = 0
							if (item.doc_type == '0') {
								item.image = require('../../../static/imgs/mine/other.png')
							} else if (item.doc_type == '1') {
								item.image = require('../../../static/imgs/mine/word.png')
							} else if (item.doc_type == '2') {
								item.image = require('../../../static/imgs/mine/excel.png')
							} else if (item.doc_type == '3') {
								item.image = require('../../../static/imgs/mine/ppt.png')
							} else if (item.doc_type == '4') {
								item.image = require('../../../static/imgs/mine/pdf.png')
							} else if (item.doc_type == '5') {
								item.image = require('../../../static/imgs/mine/txt.png')
							} else if (item.doc_type == '6') {
								item.image = require('../../../static/imgs/mine/md.png')
							}
						})
						this.list = res.docs
					}
				})
			},
			handleUploadCallback(e) {
				let _this = this
				console.log(99, e.type)
				if (['choose', 'success'].includes(e.type)) {
					_this.getData()
				}
			},
			changeMethod(data, button, index) {
				let _this = this
				if (button.title == '删除') {
					uni.showModal({
						title: '提示',
						content: '确定删除？',
						confirmColor: '#FF6E00',
						success: function(res) {
							if (res.confirm) {
								getTeacherhelperDocDel({
									id: data.id
								}).then(res => {
									_this.getData()
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			},
			clickMethod(data) {
				getTeacherhelperDocsone({
					id: data.id
				}).then(res => {
					console.log(res.doc)
					uni.downloadFile({
						url: res.doc.url,
						success: function(resp) {
							console.log(resp)
							var filePath = resp.tempFilePath;
							uni.openDocument({
								filePath: filePath,
								showMenu: true,
								success: function(res) {
									console.log('打开文档成功');
								},
								complete: function(err) {
									console.log(err);
									uni.showToast({
										icon: 'none',
										title: err.errMsg
									})
								}
							});
						}
					});
				})
			},
			upload() {
				let _that = this
				let system = uni.getSystemInfoSync();
				if (system.platform === 'android') {
					_that.quanShow = true
					plus.android.requestPermissions(['android.permission.CAMERA'],
						(e) => {
							if (e.deniedAlways.length > 0) {
								// 弹出提示框解释为何需要权限，引导用户打开设置页面开启  
								uni.showModal({
									title: '提示',
									content: '请打开手机相机相册功能（点击确定后在权限中授权相机相册功能）',
									// showCancel: false, // 不显示取消按钮
									success(res) {
										if (res.confirm) {
											var Intent = plus.android.importClass("android.content.Intent");
											var Settings = plus.android.importClass(
												"android.provider.Settings");
											var Uri = plus.android.importClass("android.net.Uri");
											var mainActivity = plus.android.runtimeMainActivity();
											var intent = new Intent();
											intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
											var uri = Uri.fromParts("package", mainActivity.getPackageName(),
												null);
											intent.setData(uri);
											mainActivity.startActivity(intent);
										}
									}
								});
							} else if (e.deniedPresent.length > 0) { //权限被临时拒绝 
								// 弹出提示框解释为何需要权限，可再次调用plus.android.requestPermissions申请权限  
								uni.showModal({
									title: '提示',
									content: '请打开手机相机功能（点击确定后在权限中授权相机功能）',
									// showCancel: false, // 不显示取消按钮
									success(res) {
										if (res.confirm) {
											var Intent = plus.android.importClass("android.content.Intent");
											var Settings = plus.android.importClass(
												"android.provider.Settings");
											var Uri = plus.android.importClass("android.net.Uri");
											var mainActivity = plus.android.runtimeMainActivity();
											var intent = new Intent();
											intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
											var uri = Uri.fromParts("package", mainActivity.getPackageName(),
												null);
											intent.setData(uri);
											mainActivity.startActivity(intent);
										}
									}
								});
							} else {
								plus.android.requestPermissions(['android.permission.READ_EXTERNAL_STORAGE'], (e) => {
									if (e.deniedAlways.length > 0) { //权限被永久拒绝  
										// 弹出提示框解释为何需要权限，引导用户打开设置页面开启  
										uni.showModal({
											title: '提示',
											content: '请打开相册存储功能（点击确定后在权限中授权相册存储功能）',
											// showCancel: false, // 不显示取消按钮
											success(res) {
												if (res.confirm) {
													var Intent = plus.android.importClass(
														"android.content.Intent");
													var Settings = plus.android.importClass(
														"android.provider.Settings");
													var Uri = plus.android.importClass(
														"android.net.Uri");
													var mainActivity = plus.android
														.runtimeMainActivity();
													var intent = new Intent();
													intent.setAction(Settings
														.ACTION_APPLICATION_DETAILS_SETTINGS);
													var uri = Uri.fromParts("package", mainActivity
														.getPackageName(),
														null);
													intent.setData(uri);
													mainActivity.startActivity(intent);
												}
											}
										});
									} else if (e.deniedPresent.length > 0) { //权限被临时拒绝 
										// 弹出提示框解释为何需要权限，可再次调用plus.android.requestPermissions申请权限  
										uni.showModal({
											title: '提示',
											content: '请打开相册存储功能（点击确定后在权限中授权相册存储功能）',
											// showCancel: false, // 不显示取消按钮
											success(res) {
												if (res.confirm) {
													var Intent = plus.android.importClass(
														"android.content.Intent");
													var Settings = plus.android.importClass(
														"android.provider.Settings");
													var Uri = plus.android.importClass(
														"android.net.Uri");
													var mainActivity = plus.android
														.runtimeMainActivity();
													var intent = new Intent();
													intent.setAction(Settings
														.ACTION_APPLICATION_DETAILS_SETTINGS);
													var uri = Uri.fromParts("package", mainActivity
														.getPackageName(),
														null);
													intent.setData(uri);
													mainActivity.startActivity(intent);
												}
											}
										});
									} else {
										//在此处可以进行操作可以使用uni.chooseImage()
										_that.quanShow = false
										_that.$refs.XeUpload.upload('file', {});

									}
								})
							}
						})
				} else {
					_that.$refs.XeUpload.upload('file', {});
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import url("../index.scss");

	.quanxian {
		background: #fff;
		padding: 24rpx;
		border-radius: 26rpx;
		position: fixed;
		top: 200rpx;
		left: 24rpx;
		right: 24rpx;

		view:first-child {
			font-size: 32rpx;
			color: #000;
			font-weight: bold;
		}

		view:last-child {
			font-size: 28rpx;
			color: #555;
			margin-top: 14rpx;
			line-height: 30rpx;
		}
	}

	.text_opblack {
		color: $uni-text-color-opblack;
	}

	.text_black {
		color: $uni-text-color-black;
	}

	.text_active {
		color: $uni-color-primary;
	}

	.text_tips {
		font-size: 24rpx;
		font-weight: 400;
		color: $uni-text-color-opblack;
	}
</style>